<?php

/**
 * uniBoard API User to User E-Mail Message
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_user_email extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore			= NULL;
	
	/**
	 * Recipients Name
	 *
	 * @var string
	 */
	 
	public $user_email_name		= NULL;

	/**
	 * Recipients E-Mail
	 *
	 * @var string
	 */
	 
	public $user_email_address	= NULL;

	/**
	 * Message Subject
	 *
	 * @var string
	 */
	 
	public $user_email_subject	= NULL;

	/**
	 * Message Content
	 *
	 * @var string
	 */
	 
	public $user_email_message	= NULL;
	
	// ------------------------------------------------------------------------
	
	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Sends E-Mail to user
	 *
	 * @param integer $user_language
	 */
	
	function sendUserEmail($user_language){
		
		// Start Mailer
		$mailer = new class_mailer($this -> appCore);
		
		// Send E-Mail
		$mailer -> sendEmail($this -> user_email_address,
		$this -> user_email_name,
		sprintf($this -> appCore -> lang['email_user_email_subject'], $this -> user_email_name),
		class_strings::parseMailText(file_get_contents(CACHE_PATH . 'lang_' . $user_language . '/email_user_to_user' . ($this -> appCore -> user_acl['can_see_users_profiles'] ? '' : '_alt') . '.txt'), array(
			'{USER_NAME}' => $this -> user_email_name,
			'{SENDER_NAME}' => $this -> appCore -> user['name'],
			'{BOARD_NAME}' => $this -> appCore -> conf['board_name'],
			'{EMAIL_SUBJECT}' => $this -> user_email_subject,
			'{EMAIL_CONTENT}' => $this -> user_email_message,
			'{SENDER_URL}' => $this -> appCore -> emailLink('act=user&user=' . $this -> appCore -> user['id'],$this -> appCore -> user['name']),
		)));
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Sets user_email_name
	 *
	 * @param string $new_value
	 */
		
	function setUserEmailName($new_value){
			
		// Check Lenght
		if ( !isset($new_value[0]))
		{
			// Set Error
			$this -> setError($this -> appCore -> lang['user_email_name_empty']);
		}
		else
		{
			// Change in-api value
			$this -> user_email_name = $new_value;
			
			// Check Lenght
			if ( mb_strlen($this -> user_email_name) > 255)
			{
				// Set Error
				$this -> setError($this -> appCore -> lang['user_email_name_too_long']);
			}	
		}
		
	}

	// ------------------------------------------------------------------------
	
	/**
	 * Sets user_email_address
	 *
	 * @param string $new_value
	 */
		
	function setUserEmailAddress($new_value){
			
		// Check Lenght
		if ( !isset($new_value[0]))
		{
			// Set Error
			$this -> setError($this -> appCore -> lang['user_email_address_empty']);
		}
		else
		{
			// Change in-api value
			$this -> user_email_address = $new_value;
			
			// Check Lenght
			if ( mb_strlen($this -> user_email_address) > 255)
			{
				// Set Error
				$this -> setError($this -> appCore -> lang['user_email_address_too_long']);
			}	
		}
		
	}

	// ------------------------------------------------------------------------
	
	/**
	 * Sets user_email_subject
	 *
	 * @param string $new_value
	 */
		
	function setUserEmailSubject($new_value){
			
		// Check Lenght
		if ( !isset($new_value[0]))
		{
			// Set Error
			$this -> setError($this -> appCore -> lang['user_email_subject_empty']);
		}
		else
		{
			// Change in-api value
			$this -> user_email_subject = $new_value;
			
			// Check Lenght
			if ( mb_strlen($this -> user_email_subject) > 100)
			{
				// Set Error
				$this -> setError($this -> appCore -> lang['user_email_subject_too_long']);
			}		
		}
		
	}

	// ------------------------------------------------------------------------
	
	/**
	 * Sets user_email_message
	 *
	 * @param string $new_value
	 */
		
	function setUserEmailMessage($new_value){
		
		// Subject not empty?
		if ( isset($new_value[0]))
		{
			// Change in-api value
			$this -> user_email_message = $new_value;
		}
		
		// Check Lenght
		if ( mb_strlen($new_value) < 50)
		{
			// Set Error
			$this -> setError($this -> appCore -> lang['user_email_message_too_short']);
		}
		else
		{			
			// Check Lenght
			if (strlen($this -> user_email_message) > 1000)
			{
				// Set Error
				$this -> setError($this -> appCore -> lang['user_email_message_too_long']);
			}		
		}
		
	}
	
}